package com.zone.gateway.sdk.domain.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.zone.gateway.common.response.Result;
import com.zone.gateway.common.response.ResultCode;
import com.zone.gateway.sdk.domain.model.entity.RedisMessageDataEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author: zongzi
 * @description: TODO
 * @date: 2025/5/7
 */
public class RedisMessageService {
    private final Logger logger = LoggerFactory.getLogger(RedisMessageService.class);

    public void publish(String centerUrl, String channel, String message) {
        RedisMessageDataEntity messageData = new RedisMessageDataEntity();
        messageData.setChannel(channel);
        messageData.setMessage(message);

        String resultStr = HttpUtil.createPost(centerUrl + "/redis/message/publish")
                .header("Content-Type", "application/json")
                .body(JSONObject.toJSONString(messageData))
                .execute()
                .body();
        Result<?> result = JSONObject.parseObject(resultStr, Result.class);
        if (!ResultCode.SUCCESS.getCode().equals(result.getCode())) {
            logger.warn("push message error channel: {} message: {}", channel, message);
        }
    }
}
